<template>
  <el-dialog
      append-to-body
      title="请配置相关信息"
      :visible.sync="dialogVisible"
      width="70%"
      @close="dialogClose"
      rihgt>
    <el-container>
      <avue-form ref="form" v-model="formModel" :option="option" @submit="submitForm"
                 @reset-change="restForm"></avue-form>
    </el-container>
  </el-dialog>
</template>

<script>
import {createCode} from "@/api/start/dataSourceTableApi";
import {createDownloadFile} from "@/utils/util";

export default {
  name: "genCode",
  data() {
    return {
      dialogVisible: false,
      formModel: {},
      row: null,
    }
  },
  methods: {
    submitForm(form, done) {
      createCode(this.row.tableSchema, this.formModel).then(res => {
            createDownloadFile(res, this.row.tableName + new Date().getTime() + ".zip")
            this.dialogClose();
          }
      ).finally(() => done())
    },
    restForm() {
      this.formModel.tableName = this.row ? this.row.tableName : '';
    },
    show(row) {
      this.dialogVisible = true;
      this.row = row;
      this.formModel.tableName = row.tableName;
      this.formModel.tablePrefix = row.tableName.indexOf("_") ? row.tableName.substring(0, row.tableName.indexOf("_")) : '';
      this.formModel.packageName = "com.start.system.modules";
    },
    dialogClose() {
      this.formModel = {};
      this.dialogVisible = false;
      this.row = null;
      this.$refs.form.resetForm();
    }
  },
  computed: {
    option() {
      return {
        menuPosition: 'right',
        column: [
          {
            label: '父级',
            prop: 'parentId',
            span: 24,
            type: "el-menu-tree-select",
            component: "el-menu-tree-select",
            showColumn: false,
          },
          {
            label: "表名",
            prop: "tableName",
            disabled: true,
            rules: [{
              required: true,
              message: "请输入表明",
              trigger: "blur"
            }
            ]
          },
          {
            label: "表名前缀",
            prop: "tablePrefix",
            value: 'start_',
            rules: [{
              required: false,
              message: "请输入表名前缀",
              trigger: "blur"
            }
            ]
          },
          {
            label: "包名",
            prop: "packageName",
            value: "com.start.system.modules",
            rules: [{
              required: true,
              message: "请输入包名",
              trigger: "blur"
            },

            ]
          },
          {
            label: "插件名称",
            prop: "moduleName",
            rules: [{
              required: true,
              message: "请输入插件名称",
              trigger: "blur",

            }]
          },
          {
            label: "作者",
            prop: "author",
            rules: [{
              required: true,
              message: "请输入作者",
              trigger: "blur"
            }
            ]
          },

        ]
      }
    },
  },

}
</script>

<style scoped>

</style>
